package app

import (
	"gin-app/app/common/request"
	"gin-app/app/common/response"
	"gin-app/app/services"
	"github.com/gin-gonic/gin"
)

// 创建书籍
func CreateBook(c *gin.Context) {
	var form request.CreateBook
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err, book := services.BookService.CreateBook(form, c.Keys["id"].(string)); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, book)
	}
}

// 获取书籍列表
func GetBookList(c *gin.Context) {
	if err, book := services.BookService.GetBookList(); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, book)
	}
}

// 查询书籍
func QueryBook(c *gin.Context) {
	var form request.QueryBook
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err, books := services.BookService.QueryBook(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, books)
	}
}
